<?php
class openapi_ctl_admin_queue extends desktop_controller{
    var $workground = "setting_tools";
    var $queues = array('local'=>'本地队列','crm'=>'CRM队列','jocs'=>'JOCS队列','wms'=>"WMS队列");

    /**
     * 
     *
     * @param  
     *
     * @return void
     * 
     * @author 张学会 <phlv@163.com>
     **/
    public function index()
    {
        foreach ($this->queues as $queue=>$lvaue) {
            $mq = new openapi_queue_rabbitmq();
            $str .= $queue.':'.$mq->getMsgCount($queue).'<br>';
        }
        $this->pagedata['show'] = $str;
        $this->page("admin/queue/show.html");
    }
    /**
     * undocumented function
     *
     * @param  
     *
     * @return void
     * 
     * @author 张学会 <phlv@163.com>
     **/
    public function manger()
    {
        #$queues = array('local'=>'本地队列','crm'=>'CRM队列','jocs'=>'JOCS队列','wms'=>"WMS队列");
        $scriptDir = app::get('openapi')->app_dir.'/crontab/';
        foreach ($this->queues as $queue=>$name) {
            $res['name'] = $name;
            $res['flag'] = $queue;
            $res['num'] = $this->readLine($scriptDir.$queue.'.log');
            $show[] = $res;
        }
        $this->pagedata['queues'] = $show;
        $this->page("admin/queue/manger.html");
    }
    /**
     * undocumented function
     *
     * @param  
     *
     * @return void
     * 
     * @author 张学会 <phlv@163.com>
     **/
    public function save()
    {
        $this->begin("index.php?app=openapi&ctl=admin_queue&act=manger");
        $limit = 10;
        $error = false;
        $scriptDir = app::get('openapi')->app_dir.'/crontab/';
        #var_dump($_POST);
        foreach ($this->queues as $queue => $name) {
            $_POST[$queue] = (int)$_POST[$queue];
            if ($_POST[$queue]<0 || $_POST[$queue]>10) {
                $error = $name."只能是大于等于0，小于等于10的整数";
                break;
            }
        }
        if ($error) {
            $this->end(false,$error);
        }
        foreach ($this->queues as $queue => $name) {
            $this->writeLine($scriptDir.$queue.'.log',$_POST[$queue]);
        }
        $this->end(true,"保存成功");
    }
    /**
     * undocumented function
     *
     * @param  
     *
     * @return void
     * 
     * @author 张学会 <phlv@163.com>
     **/
    public function readLine($filePath)
    {
        $fs = file_get_contents($filePath);
        if (!$fs) {
            return 0;
        }
        $fs = explode("\n", $fs);
        return count($fs);
    }
    /**
     * 
     *
     * @param  
     *
     * @return void
     * 
     * @author 张学会 <phlv@163.com>
     **/
    public function writeLine($filePath,$line)
    {
        $data = array();
        for ($i=0; $i < $line; $i++) { 
            $data[] = date('Y-m-d H:i:s',time());
        }
        if ($line == 0) {
            $content = "";
        }else{
            $content = implode("\n", $data);
        }
        error_log(''.var_export($content,1)."\r\n",3,__FILE__.'.log');
        return file_put_contents($filePath, $content);
    }
    
}